﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class CreateCustomer : Form
    {
        string passCustomerID;
        public CreateCustomer()
        {
            InitializeComponent();
            lblshowcustomerid.Text = "CU" + DateTime.Now.GetHashCode();
            List<string> countries = getCountryList();
            cboxcountry.DataSource = countries;
            while(ManageCustomerDBManager.TestWhetherCustomerIDExists(lblshowcustomerid.Text) == true)
            {
                lblshowcustomerid.Text = "CU" + DateTime.Now.GetHashCode();
            }
            passCustomerID = lblshowcustomerid.Text;
        }

        private void btncreate_Click(object sender, EventArgs e)
        {
            string dob = cboxday.SelectedItem + "/" + cboxmonth.SelectedItem + "/" + cboxyear.SelectedItem;
            DateTime dt;
            if (!DateTime.TryParse(dob, out dt))
            {
                MessageBox.Show("Invalid Date, please check date of birth");
                return;
            }

            if(tbxfirstname.Text.Equals("") || tbxlastname.Text.Equals("") || tbxhomeaddress.Text.Equals("") || tbxpostalcode.Text.Equals("") || cboxcountry.SelectedIndex == 0 || tbxcontactno.Text.Equals("") || tbxemail.Text.Equals("") || tbxnric.Text.Equals(""))
            {
                MessageBox.Show("Please ensure all the information are filled in");
                return;
            }

            foreach(Customer c in ManageCustomerDBManager.GetAllCustomerDetails())
            {
                if (c.Nric.Equals(tbxnric.Text, StringComparison.InvariantCultureIgnoreCase))
                {
                    MessageBox.Show("Please check your nric whether is it correct");
                    return;
                }
            }

            string tempCustomerID = lblshowcustomerid.Text;
            while (ManageCustomerDBManager.TestWhetherCustomerIDExists(tempCustomerID) == true)
            {
                tempCustomerID = "CU" + DateTime.Now.GetHashCode();
            }

            Customer newCustomer = new Customer();
            newCustomer.CustomerID = passCustomerID;
            newCustomer.FirstName = tbxfirstname.Text;
            newCustomer.LastName = tbxlastname.Text;
            newCustomer.HomeAddress = tbxhomeaddress.Text;
            newCustomer.PostalCode = Convert.ToInt32(tbxpostalcode.Text);
            newCustomer.Country = cboxcountry.SelectedItem.ToString();
            newCustomer.Dob = Convert.ToDateTime(dob);
            newCustomer.ContactNo = Convert.ToInt32(tbxcontactno.Text);
            newCustomer.Email = tbxemail.Text;
            newCustomer.Nric = tbxnric.Text;
            if (ManageCustomerDBManager.CreateNewCustomer(newCustomer) == 1)
            {
                MessageBox.Show("Create Was Sucessful, Customer ID: " + newCustomer.CustomerID);
                this.Hide();
            }
            
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        public List<string> getCountryList()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

            cultureList.Add("-- Select Country --");

            foreach (CultureInfo culture in cultures)
            {
                CultureTypes ct = culture.CultureTypes;
                String s = ct.ToString();
                if (!s.Contains("NeutralCultures"))
                {
                    // check if it's not a invariant culture
                    if (culture.LCID != 127)
                    {
                        RegionInfo region = new RegionInfo(culture.LCID);
                        // add countries that are not in the list
                        if (!(cultureList.Contains(region.EnglishName)))
                        {
                            cultureList.Add(region.EnglishName);
                        }
                    }
                }
            }
            cultureList.Sort(); // sort alphabetically
            return cultureList;
        }

        private void tbxCheckAllForNumbers_TextChanged(object sender, EventArgs e) // int only generalized method
        {
            Exception X = new Exception();
            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    int x = int.Parse(T.Text);
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }
    }
}
